#CROSS_COMPILE 	?= arm-none-eabi-
CROSS_COMPILE 	?= arm-linux-gnueabihf-

TARGET		  	?= freertos

CC 				:= $(CROSS_COMPILE)gcc
LD				:= $(CROSS_COMPILE)ld
OBJCOPY 		:= $(CROSS_COMPILE)objcopy
OBJDUMP 		:= $(CROSS_COMPILE)objdump
SIZE 		:= $(CROSS_COMPILE)size

#LIBPATH			:= -lgcc -L /usr/local/arm/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/lib/gcc/arm-linux-gnueabihf/4.9.4
LIBPATH			:= -lgcc -L D:/gcc-arm-linux-gnueabihf-6.2.1/lib/gcc/arm-linux-gnueabihf/6.2.1
#LIBPATH			:= -lgcc -L D:/2019-q4-major/lib/gcc/arm-none-eabi/9.2.1

# 中间件编译选项
FREERTOS_SUPPORT ?= 1
LITTLEVGL_SUPPORT ?= 1
PLOOC_SUPPORT ?= 1

#project选择
#include project/littlevgl-demo/makefile.mk
include project/littlevgl/makefile.mk
#include project/demo/makefile.mk

include freertos/makefile.rtos
include middleware/makefile.middleware

INCDIRS 		+= imx6ul \
					 project/common \
				   stdio/include \
				   bsp/clk \
				   bsp/led \
				   bsp/delay  \
				   bsp/beep \
				   bsp/gpio \
				   bsp/key \
				   bsp/exit \
				   bsp/int \
				   bsp/epittimer \
				   bsp/keyfilter \
				   bsp/uart \
				   bsp/lcd \
				   bsp/rtc \
				   bsp/i2c \
				   bsp/ap3216c \
				   bsp/spi \
				   bsp/icm20608 \
				   bsp/touchscreen \
				   bsp/backlight
				   			   
SRCDIRS			+=stdio/lib \
					 project/common \
				   bsp/clk \
				   bsp/led \
				   bsp/delay \
				   bsp/beep \
				   bsp/gpio \
				   bsp/key \
				   bsp/exit \
				   bsp/int \
				   bsp/epittimer \
				   bsp/keyfilter \
				   bsp/uart \
				   bsp/lcd \
				   bsp/rtc \
				   bsp/i2c \
				   bsp/ap3216c \
				   bsp/spi \
				   bsp/icm20608 \
				   bsp/touchscreen \
				   bsp/backlight
				   
				   
INCLUDE			:= $(patsubst %, -I %, $(INCDIRS))

SFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.S))
CFILES			:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

SFILENDIR		:= $(notdir  $(SFILES))
CFILENDIR		:= $(notdir  $(CFILES))

SOBJS			:= $(patsubst %, obj/%, $(SFILENDIR:.S=.o))
COBJS			:= $(patsubst %, obj/%, $(CFILENDIR:.c=.o))
OBJS			:= $(SOBJS) $(COBJS)

VPATH			:= $(SRCDIRS)
MYFLAG += -DMY_DEBUG
#MYFLAG ?=
.PHONY: clean img
	
$(TARGET).bin : $(OBJS)
	$(LD) -T$(LDS_FILE) -o $(TARGET).elf $^ $(LIBPATH)
	$(OBJCOPY) -O binary -S $(TARGET).elf $@
	$(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis
	$(SIZE) $(TARGET).elf
	mkimage -n ./tools/imximage.cfg.cfgtmp -T imximage -e 0x87800000 -d $(TARGET).bin $(TARGET).imx
	dd if=/dev/zero of=1k.bin bs=1024 count=1
	cat 1k.bin $(TARGET).imx > $(TARGET).img

img:
	mkimage -n ./tools/imximage.cfg.cfgtmp -T imximage -e 0x87800000 -d $(TARGET).bin $(TARGET).imx
	dd if=/dev/zero of=1k.bin bs=1024 count=1
	cat 1k.bin $(TARGET).imx > $(TARGET).img

$(SOBJS) : obj/%.o : %.S
	$(CC) -Wall -nostdlib -mfpu=neon-vfpv4 -fno-builtin $(MYSFLAG) -c -O2  $(INCLUDE) -o $@ $<

$(COBJS) : obj/%.o : %.c
	$(CC) -Wall -march=armv7-a -mfpu=neon-vfpv4 -mfloat-abi=hard -Wa,-mimplicit-it=thumb -nostdlib -fno-builtin $(MYFLAG) -c -O2  $(INCLUDE) -o $@ $<


clean:
	rm -rf $(TARGET).elf $(TARGET).dis $(TARGET).bin $(COBJS) $(SOBJS)

	
